//
//  RVRailwayOrderListController.swift
//  AirTravel
//
//  Created by apple on 2021/11/17.
//

import UIKit
import HandyJSON
class RVRailwayOrderListController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    
    var status: String
    
    var dataSource: [RVRailwayOrderList] = []
    
    init(status: String) {
        self.status = status
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        confTable()
    }

    func confTable(){
        tableView.separatorStyle = .none
        tableView.rowHeight = 220
        tableView.showsVerticalScrollIndicator=false
        tableView.register(UINib(nibName: "RVFlightOrderListCell", bundle: nil), forCellReuseIdentifier: "RVFlightOrderListCell")
        getDatas()
    }
    
    
    func getDatas(){
        var parameters: [String: Any] = [String: Any]()
        parameters["status"] = status
        parameters["is_all"] = "0"
        YProgressHUD.show()
        NetWorkRequest(API1.getRailwayOrderList(parameters: parameters)) { json in
            
            YProgressHUD.dismiss()
            guard let data = JSONDeserializer<RVRailwayData>.deserializeFrom(dict: json.dictionaryObject) else{
                return
            }
            self.dataSource = data.data
            self.tableView.reloadData()
            
        } failed: { msg in
            print("=====")
            YProgressHUD.dismiss()

        } errorResult: {
            YProgressHUD.dismiss()
            print("=====")

        }

    }
    
}


extension RVRailwayOrderListController: UITableViewDelegate, UITableViewDataSource{
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:RVFlightOrderListCell=tableView.dequeueReusableCell(withIdentifier: "RVFlightOrderListCell", for: indexPath) as! RVFlightOrderListCell
        cell.bind(railway: dataSource[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

